package day15.HomeWork;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Scanner;


public abstract class ServicePackage implements Serializable, CallService, SendService, NetService {
    double price;

    DecimalFormat df = new DecimalFormat("#.##");
    static Scanner sc = new Scanner(System.in);
    public abstract String showInfo();
    public ServicePackage() {
    }
}


class TalkPackage extends ServicePackage implements Serializable {
    int talkTime;
    int smsCount;
    int flow;

    public TalkPackage(double price, int talkTime, int smsCount, int flow) {
        super.price = price;
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    public String showInfo() {
        return "话痨套餐:" + price + "元,通话时长:" + talkTime + "分钟,短信条数:" + smsCount + "条,上网流量:" + flow + "GB";
    }

    @Override
    public void call(int count, MobileCard card) {
        //System.out.println("打电话跟客户谈生意,用了" + count + "分钟");
        card.setRealTalkTime(count);    //用户真实通话时间
        int t = talkTime -= count;  //剩余通话时间
        if (talkTime < 0) {
            System.out.println(card.getUserName() + "通话时间剩余0分钟,继续通话按0.2元/分钟计费");
            double consumMoney = Math.abs(t) * 0.2;
            talkTime = 0;
            double v = card.getConsumAmount() + consumMoney;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumMoney);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                    card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                    System.out.println(card.getMoney());
                    System.out.println("充值金额不够,请在次充值!");
                    System.out.println("请输充值的金额");
                    double addM = sc.nextDouble();
                    card.setMoney(Double.parseDouble(df.format(card.getMoney() + addM)));
                }

                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外通话:" + Math.abs(t) + "分钟,套餐外计费:" + df.format(consumMoney) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + talkTime + "分钟");
        }
    }

    @Override
    public void netPlay(int f, MobileCard card) {
        //System.out.println("上网用了" + f + "GB");
        card.setRealFlow(f);    //用户真正使用的流量
        int ff = flow -= f;  //剩余流量
        if (flow < 0) {
            System.out.println(card.getUserName() + "流量剩余0GB,继续使用按1元/GB计费");
            double consumFlow = Math.abs(ff);
            flow = 0;
            double v = card.getConsumAmount() + consumFlow;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumFlow);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外流量:" + Math.abs(ff) + "GB,套餐外计费:" + df.format(consumFlow) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + flow + "GB");
        }
    }

    @Override
    public void send(int count, MobileCard card) {
        //System.out.println("跟朋友聊天,共发送了" + count + "条消息");
        card.setRealSMSCount(count);
        int i = smsCount -= count;    //剩余的消息数量
        if (smsCount < 0) {
            System.out.println(card.getUserName() + "发送消息剩余0条,继续发消息按0.1元/条计费");
            double consumMoney = Math.abs(i) * 0.1;
            smsCount = 0;
            double v = card.getConsumAmount() + consumMoney;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumMoney);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外发了:" + Math.abs(i) + "条消息,套餐外计费:" + df.format(consumMoney) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + smsCount + "条");
        }
    }
}


class SuperPackage extends ServicePackage implements Serializable {
    int talkTime;
    int smsCount;
    int flow;

    public SuperPackage(double price, int talkTime, int smsCount, int flow) {
        super.price = price;
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    public String showInfo() {
        return "超人套餐:" + price + "元,通话时长:" + talkTime + "分钟,短信条数:" + smsCount + "条,上网流量:" + flow + "GB";
    }

    @Override
    public void call(int count, MobileCard card) {
        //System.out.println("打电话跟客户谈生意,用了" + count + "分钟");
        card.setRealTalkTime(count);    //用户真实通话时间
        int t = talkTime -= count;  //剩余通话时间
        if (talkTime < 0) {
            System.out.println(card.getUserName() + "通话时间剩余0分钟,继续通话按0.2元/分钟计费");
            double consumMoney = Math.abs(t) * 0.2;
            talkTime = 0;
            double v = card.getConsumAmount() + consumMoney;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumMoney);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外通话:" + Math.abs(t) + "分钟,套餐外计费:" + df.format(consumMoney) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + talkTime + "分钟");
        }
    }

    @Override
    public void netPlay(int f, MobileCard card) {
        //System.out.println("上网用了" + f + "GB");
        card.setRealFlow(f);    //用户真正使用的流量
        int ff = flow -= f;  //剩余流量
        if (flow < 0) {
            System.out.println(card.getUserName() + "流量剩余0GB,继续使用按1元/GB计费");
            double consumFlow = Math.abs(ff);
            flow = 0;
            double v = card.getConsumAmount() + consumFlow;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumFlow);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外流量:" + Math.abs(ff) + "GB,套餐外计费:" + df.format(consumFlow) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + flow + "GB");
        }
    }

    @Override
    public void send(int count, MobileCard card) {
        //System.out.println("跟朋友聊天,共发送了" + count + "条消息");
        card.setRealSMSCount(count);
        int i = smsCount -= count;    //剩余的消息数量
        if (smsCount < 0) {
            System.out.println(card.getUserName() + "发送消息剩余0条,继续发消息按0.1元/条计费");
            double consumMoney = Math.abs(i) * 0.1;
            smsCount = 0;
            double v = card.getConsumAmount() + consumMoney;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumMoney);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外发了:" + Math.abs(i) + "条消息,套餐外计费:" + df.format(consumMoney) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + smsCount + "条");
        }
    }
}


class NetPackage extends ServicePackage implements Serializable {
    int talkTime;
    int smsCount;
    int flow;

    public NetPackage(double price, int talkTime, int smsCount, int flow) {
        super.price = price;
        this.talkTime = talkTime;
        this.smsCount = smsCount;
        this.flow = flow;
    }

    public String showInfo() {
        return "网虫套餐:" + price + "元,通话时长:" + talkTime + "分钟,短信条数:" + smsCount + "条,上网流量:" + flow + "GB";
    }

    @Override
    public void call(int count, MobileCard card) {
        card.setRealTalkTime(count);    //用户真实通话时间
        int t = talkTime -= count;  //剩余通话时间
        if (talkTime < 0) {
            System.out.println(card.getUserName() + "通话时间剩余0分钟,继续通话按0.2元/分钟计费");
            double consumMoney = Math.abs(t) * 0.2;
            talkTime = 0;
            double v = card.getConsumAmount() + consumMoney;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumMoney);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外通话:" + Math.abs(t) + "分钟,套餐外计费:" + df.format(consumMoney) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + talkTime + "分钟");
        }
    }

    @Override
    public void netPlay(int f, MobileCard card) {
        //System.out.println("上网用了" + f + "GB");
        card.setRealFlow(f);    //用户真正使用的流量
        int ff = flow -= f;  //剩余流量
        if (flow < 0) {
            System.out.println(card.getUserName() + "流量剩余0GB,继续使用按1元/GB计费");
            double consumFlow = Math.abs(ff);
            flow = 0;
            double v = card.getConsumAmount() + consumFlow;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumFlow);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外流量:" + Math.abs(ff) + "GB,套餐外计费:" + df.format(consumFlow) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + flow + "GB");
        }
    }

    @Override
    public void send(int count, MobileCard card) {
        //System.out.println("跟朋友聊天,共发送了" + count + "条消息");
        card.setRealSMSCount(count);
        int i = smsCount -= count;    //剩余的消息数量
        if (smsCount < 0) {
            System.out.println(card.getUserName() + "发送消息剩余0条,继续发消息按0.1元/条计费");
            double consumMoney = Math.abs(i) * 0.1;
            smsCount = 0;
            double v = card.getConsumAmount() + consumMoney;
            card.setConsumAmount(v);
            card.setMoney(card.getMoney() - consumMoney);
            if (card.getMoney() < 0) {
                System.out.println("欠费了,请充值!(欠费:" + df.format(Math.abs(card.getMoney())) + "元)");
                System.out.println("请输充值的金额");
                double addMon = sc.nextDouble();
                while (true) {
                    if (addMon < Double.parseDouble(df.format(Math.abs(card.getMoney())))) {
                        card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
                        System.out.println(card.getMoney());
                        System.out.println("充值金额不够,请在次充值!");
                        System.out.println("请输充值的金额");
                        addMon = sc.nextDouble();
                    } else break;
                }
                card.setMoney(Double.parseDouble(df.format(card.getMoney() + addMon)));
            } else {
                System.out.println("套餐外发了:" + Math.abs(i) + "条消息,套餐外计费:" + df.format(consumMoney) + "元,一共消费" + df.format(v) + "元,剩余" + df.format(card.getMoney()) + "元");
            }
        } else {
            System.out.println(card.getUserName() + "剩余:" + smsCount + "条");
        }
    }
}

interface CallService {
    void call(int Count, MobileCard card);
}

interface SendService {
    void send(int count, MobileCard card);
}

interface NetService {
    void netPlay(int flow, MobileCard card);
}